package com.buho29.rcmanager.bd;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

public class TableDrive extends BaseActiveRecord  {

	public static final String SQL_TABLE_NAME = "drive";
	
	public static final String SQL_CREATE_TABLE = "create table " + SQL_TABLE_NAME + " (" +
				"_id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT," +
			    "model_id INTEGER NOT NULL," +
			    "powerItem_id INTEGER NOT NULL," +
			    "time INTEGER NOT NULL," +
	    		"date INTEGER NOT NULL" +
		    ");";
	
	public static final String COL_DATE = "date";
	public static final String COL_TIME = "time";
	public static final String COL_MODEL_ID = "model_id";
	public static final String COL_POWER_ITEM_ID = "powerItem_id";
	
	public static final String[] COLUMNS = new String[] { 
		COL_ROW_ID, COL_MODEL_ID, COL_POWER_ITEM_ID, COL_DATE ,COL_TIME 
	};

	private long mDate;
	public long getDate() {return mDate;}
	
	private int mTime;
	public int getTime() {return mTime;}
	public void setTime(int sg) {mTime = sg;}
		
	private long mModelId;
	public long getModelId() {return mModelId;}
	public void setModelId(long modelId) {mModelId = modelId;}

	private long mPowerItemId;
	public long getPowerItemId() {return mPowerItemId;}
	public void setPowerItemId(long powerItem) {mPowerItemId = powerItem;}
	
	@Override
	public Boolean load() {
		Cursor cursor = mDb.query(true, SQL_TABLE_NAME,COLUMNS,
				COL_ROW_ID + "=" + mId,
				null, null,null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			loadCursor(cursor);
			
			cursor.close();
			
			return true;
		}
		return false;
		
	}

	@Override
	public long save() {
		ContentValues values = new ContentValues();
		values.put(COL_MODEL_ID,mModelId);
		values.put(COL_POWER_ITEM_ID,mPowerItemId);
		values.put(COL_TIME,mTime);
		
		if (mId < 1) {
			values.put(COL_DATE,new Date().getTime());
			return mDb.insert(SQL_TABLE_NAME, null, values);
		} else { 
			values.put(COL_DATE,mDate);
			return mDb.update(SQL_TABLE_NAME, values, COL_ROW_ID      + "=" + mId, null);
		}
		
	}
	
	@Override
	public boolean delete() {
		return mDb.delete(SQL_TABLE_NAME, COL_ROW_ID + "=" + mId,     null) > 0;
	}
	
	@Override
	public Cursor retrieveAll() {
		return mDb.query(SQL_TABLE_NAME, COLUMNS, null, null,
				null, null, null);
	}
	
	public void deleteLast(long powerItemID ,long modelID){
		mDb.execSQL(
			"DELETE FROM drive WHERE date = (SELECT MAX(date) FROM drive WHERE model_id=" +modelID+
			" AND powerItem_id ="+ powerItemID + ")");
	}
	
	public Cursor fetchPower(long powerItemID ,long modelID){
		Cursor tmp = mDb.rawQuery(
			"SELECT drive.time FROM drive WHERE drive.model_id=? AND drive.powerItem_id=?",
			new String[]{String.valueOf(modelID),String.valueOf(powerItemID)}
		);
		tmp.moveToFirst();	
		return tmp;
	}
	
	public Cursor fetchDrivePowerItem(long PowerItemID){
		Cursor tmp = mDb.rawQuery(
			"SELECT drive.time FROM drive WHERE drive.powerItem_id=?",
			new String[]{String.valueOf(PowerItemID)}
		);
		tmp.moveToFirst();	
		return tmp;
	}
	
	public Cursor fetchModels(long PowerItemID) {
		Cursor tmp = mDb.rawQuery(
			"SELECT DISTINCT model_id FROM drive WHERE drive.powerItem_id=?",
			new String[]{String.valueOf(PowerItemID)}
		);
		tmp.moveToFirst();	
		return tmp;
	}
	
	public void deleteEmpty() {
		mDb.execSQL(
			"DELETE FROM drive WHERE drive.powerItem_id NOT IN " +
			"(SELECT powerItem._id from powerItem) AND drive.model_id NOT IN " +
			"(SELECT model._id from model)"
		);
	}
	@Override
	public void loadCursor(Cursor cursor) {
		mModelId = cursor.getLong(cursor.getColumnIndexOrThrow(COL_MODEL_ID));
		mPowerItemId = cursor.getLong(cursor.getColumnIndexOrThrow(COL_POWER_ITEM_ID));
		mDate = cursor.getLong(cursor.getColumnIndex(COL_DATE));
		mTime = cursor.getInt(cursor.getColumnIndex(COL_TIME));
	}
}
